;; Tests of the Non-robust computational geometry functions
;; Copyright (C) 2021  Rocks Mazama
;; 
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; 
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;; 
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(ns mazama.compgeom.poly-test
  (:require
   [clojure.test :refer [deftest is testing]]
   [mazama.compgeom.poly :refer :all]))

;;;; NOTICE
;;;;
;;;; It is assumed that
;;;; the x-axis increases to the right and
;;;; the y-axis increases DOWNWARD

(deftest area-of-polygon-test
  (testing "empty sequence"
    (is (== 0 (area-of-polygon []))))
  (testing "just one point"
    (is (== 0 (area-of-polygon [[2 3]]))))
  (testing "just two points"
    (is (== 0 (area-of-polygon [[2 3] [4 5]]))))
  (testing "counterclockwise triangle"
    (is (== 1/2 (area-of-polygon [[0 0] [0 1] [1 1]]))))
  (testing "clockwise triangle"
    (is (== -1/2 (area-of-polygon [[-1 -1] [-2 -1] [-2 -2]]))))
  (testing "colinear 3 points"
    (is (== 0 (area-of-polygon [[1/2 1/2] [1 1] [2 2]]))))
  (testing "self-intersecting polygon"
    (is (== 0 (area-of-polygon [[0 0] [1 1] [1 0] [0 1]]))))
  (testing "Part 3: https://www.wikihow.com/Calculate-the-Area-of-a-Polygon"
    ;; In our coordinate system the polygon is clockwise, hence negative area
    (is (== -60 (area-of-polygon [[-3 -2] [-1 4] [6 1] [3 10] [-4 9]]))))
  (testing "explicitly closed"
    ;; Using the previous polygon, but reversed and explicitly closed
    (is (== 60 (area-of-polygon [[-4 9] [3 10] [6 1] [-1 4] [-3 -2] [-4 9]])))))

(deftest length-of-polyline-squared-test
  (testing "empty sequence"
    (is (== 0 (length-of-polyline-squared []))))
  (testing "just one point"
    (is (== 0 (length-of-polyline-squared [[2 3]]))))
  (testing "just two points"
    (is (== 25 (length-of-polyline-squared [[2 3] [5 7]]))))
  (testing "some points"
    (is (== (* 25 5) (length-of-polyline-squared [[2 3] [5 7] [2 3] [5 7] [2 3] [5 7]])))))

(deftest polygon==-test
  (testing "duplicate points"
    (is (polygon== [[0 0] [0.0 0.0] [1 0] [1 0] [1 1] [1 1] [0 1] [0 1]]
                   [[1 1] [1 0] [1 0] [0 0] [0 0] [0 1] [0 1] [1 1]]))))
